package com.eascs.banknet.task;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.banknet.boc.base.BOCUtil;
import com.eascs.banknet.service.BankService;

@Component("bocTask")
public class BocTask {
	private static final Logger logger = LoggerFactory.getLogger(BocTask.class);
	
	@Autowired
	@Qualifier("bocB01Service")
	private BankService bocB01Service;
	
	@Autowired
	@Qualifier("bocB02Service")
	private BankService bocB02Service;
	
	@Autowired
	@Qualifier("bocS01Service")
	private BankService bocS01Service;
	
	@Autowired
	@Qualifier("boc002Service")
	private BankService boc002Service;
	
	@Autowired
	@Qualifier("boc003Service")
	private BankService boc003Service;
	
	@Autowired
	@Qualifier("boc004Service")
	private BankService boc004Service;
	
	@Autowired
	@Qualifier("boc001V1Service")
	private BankService boc001Service;
	
	@Autowired
	@Qualifier("boc006Service")
	private BankService boc006Service;
	
	@Autowired
	@Qualifier("bocS02Service")
	private BankService bocS02Service;
	
	@Autowired
	@Qualifier("bOCZXBGDownloadService")
	private BankService bOCZXBGDownloadService;
	
	@Autowired
	@Qualifier("bOCDKHTDownloadService")
	private BankService bOCDKHTDownloadService;
	
	@Autowired
	@Qualifier("bOCUserFileUploadService")
	private BankService bOCUserFileUploadService;
	
	@Autowired
	@Qualifier("bocFtpParseService")
	private BankService bocFtpParseService;
	
	@Autowired
	private BOCUtil bocUtil;
	
	//启动一个线程，监听中行004请求
	@PostConstruct
	public void startService() {
		String socketHost = bocUtil.getBocSocketInfo().getHref();
		//正式环境暂时不跑004
		//if("26.127.253.48".equals(socketHost)){
			new Thread(new Runnable() {
				@Override
				public void run() {
					logger.info("启动监听服务");
					boc004Service.operate();
				}
			}).start();
		//}
	}
	
	/**
	 * BOC002交易，查询贷款状态
	 * */
	@Scheduled(cron = "0 0/2 * * * ?")
	public void doBOC002(){
		try {
			boc002Service.operate();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * BOCSO1交易，额度测算
	 * */
	@Scheduled(cron = "12 0/3 * * * ?")
	public void doBOCS01(){
		try {
			bocS01Service.operate();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * BOCSO2借款合同及借据生成
	 * */
	@Scheduled(cron = "24 0/3 * * * ?")
	public void doBOCS02(){
		try {
			bocS02Service.operate();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * BOCBO1交易，贷前调查
	 * */
	@Scheduled(cron = "36 0/2 * * * ?")
	public void doBOCB01(){
		//用于下一次轮询开始位置的标记
		try {
			bocB01Service.operate();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * BOC0O1交易，贷款申请
	 * */
	@Scheduled(cron = "48 0/2 * * * ?")
	public void doBOC001(){
		try {
			boc001Service.operate();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * BOC003交易，查询放款信息
	 * */
	@Scheduled(cron = "48 0/3 * * * ?")
	public void doBOC003(){
		try {
			boc003Service.operate();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 中行征信报告拉取
	 * */
	@Scheduled(cron = "0 0/5 * * * ?")
	public void doBocZXBGPull(){
		try {
			bOCZXBGDownloadService.operate();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 电子合同拉取
	 * */
	@Scheduled(cron = "0 0/4 * * * ?")
	public void doBocDZHTPull(){
		try {
			bOCDKHTDownloadService.operate();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 申报文件上传
	 * */
	@Scheduled(cron = "0 0/2 * * * ?")
	public void doBocUserFilePull(){
		try {
			bOCUserFileUploadService.operate();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 读取中行贷后文件,每天23点执行一次
	 * */
	@Scheduled(cron = "0 0 23 * * ?")
	public void doFtpParse(){
		try {
			bocFtpParseService.operate();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
